using UnityEngine;
using System.Collections;

public class BSPDungeon : IDungeonMapGenerator
{

    public BSPTree treeData;
	public int dungeonWidth = 256;
	public int dungeonHeight = 256;
    PseudoRandom.MersenneTwister random;

    public BSPDungeon()
    {
        random = new PseudoRandom.MersenneTwister((ulong)System.DateTime.Now.Ticks);
        treeData = new BSPTree(new BSPRectangle(0, 0, dungeonWidth, dungeonHeight), random);
    }
	
	public void GenerateMap(Dungeon dungeon)
	{
		treeData.ClearRooms();
		treeData.GenerateRooms();
		treeData.GenerateCorridors();
	}

    public void GenerateTestData()
    {
        BSPRectangle splitPoint;

        splitPoint = new BSPRectangle(256, 0, 1, 512);
        treeData.oldSplits.Add(splitPoint);

        splitPoint = new BSPRectangle(0, 240, 256, 1);
        treeData.newSplits.Add(splitPoint);

        splitPoint = new BSPRectangle(256, 280, 256, 1);
        treeData.newSplits.Add(splitPoint);

        BSPNode testNode;

        testNode = new BSPNode(new BSPRectangle(5, 5, 30, 30), random);

        treeData.root.AddRoom(new BSPRectangle(5, 5, 30, 30));
        treeData.root.AddRoom(new BSPRectangle(15, 45, 30, 30, BSPRectangle.RectangleType.Corridor));
        treeData.root.AddRoom(new BSPRectangle(25, 85, 30, 30));
        treeData.root.AddRoom(new BSPRectangle(35, 125, 30, 30));
    }

    public void Reset()
    {
        random = new PseudoRandom.MersenneTwister((ulong)System.DateTime.Now.Ticks);
        treeData = new BSPTree(new BSPRectangle(0, 0, dungeonWidth, dungeonHeight), random);
    }

    public void GenerateRooms()
    {
        treeData.ClearRooms();
        treeData.GenerateRooms();
    }

    public void GenerateCorridors()
    {
        treeData.GenerateCorridors();
    }

}
